// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var BasePkCardCtr = require('./BasePkCardCtr');
var PkSound = require('./../PkSound');
cc.Class({
    extends: BasePkCardCtr,
    properties: {
        iconLandlord: {
            default: null,
            type: cc.Node,
            displayName: '地主标签'
        }
    },
    onLoad() {
        this.isCanTouch = true;
        this.resetState();
        this.node.on(cc.Node.EventType.TOUCH_START, function(event) {
            if (!this.isCanTouch) {
                return;
            }
            this.touchStart();
            this.doSwitch()
            this.doLockChoice();
            PkSound.choiceCard();
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function(event) {
            if (!this.isCanTouch) {
                return;
            }
            this.touchMove(event);
        }, this);

        this.node.on(cc.Node.EventType.TOUCH_END, function(event) {
            if (!this.isCanTouch) {
                return;
            }
            this.touchCancel();
        }, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function(event) {
            if (!this.isCanTouch) {
                return;
            }
            this.touchCancel();
        }, this);
    },
    setCanTouch(_is) {
        this.isCanTouch = _is;
    },
    /***是否需要显示地主 */
    setActiveLandlord(_isActive) {
        this.iconLandlord.active = _isActive;
    },
    resetState() {
        this.setChoice(false);
        this.setActiveLandlord(false);
        this.doUnLockChoice();
    },
    /***监听触摸开始回调 */
    registerTouchStart(_touchStart) {
        this.touchStart = _touchStart;
    },
    /***监听触摸取消回调 */
    registerTouchCancel(_touchCancel) {
        this.touchCancel = _touchCancel;
    },
    /***监听触摸取消回调 */
    registerTouchMove(_touchMove) {
        this.touchMove = _touchMove;
    },
    /**发牌动画 */
    actSendCard(time) {
        var action = cc.moveTo(time, this.getPositionActual());
        action.easing(cc.easeElasticOut(time * 2.5));
        this.doAction(action);
    },
    /***微移 */
    actResetPosition(_pos, time, _isEasing) {
        _isEasing = null == _isEasing ? true : _isEasing;

        if (false == this.isCanTouch) {
            return;
        }
        time = null == time ? 0.4 : time;
        var action = cc.spawn([cc.moveTo(time, _pos),
            cc.scaleTo(time, 1)
        ]);
        if (_isEasing) {
            action.easing(cc.easeElasticOut(time * 5));
        }
        this.doAction(action);
    },
    /***播放动画至选中位置*/
    actPlayChoice() {
        if (false == this.isCanTouch) {
            return;
        }
        this.setChoice(true);
        var posActual = this.getPositionActual();
        var moveToPos = cc.v2(posActual.x, posActual.y + this.getChoiceHeight());
        var action = cc.moveTo(this.getChoiceTime(), moveToPos);
        action.easing(cc.easeElasticOut(this.getChoiceTime() * 6.5));
        this.doAction(action);
    },
    /***播放动画至默认位置*/
    actRemoveChoice() {
        if (false == this.isCanTouch) {
            return;
        }

        this.setChoice(false);
        var posActual = this.getPositionActual();
        var time = this.getUnChoiceTime();
        var action = cc.spawn([cc.moveTo(time, posActual),
            cc.scaleTo(time, 1)
        ]);
        action.easing(cc.easeElasticOut(this.getUnChoiceTime() * 5.5));
        this.doAction(action);
    },
    /**================================================================================================== */
    getChoiceTime() {
        return 0.5;
    },
    getUnChoiceTime() {
        return 0.45;
    },
    getChoiceHeight() {
        var size = this.getContentSize();
        return size.height * 0.1;
    },
    setChoice(_isChoice) {
        this.isChoice = _isChoice;
    },
    getIsChoice() {
        return this.isChoice;
    },
    doSwitch() {
        if (this.isLock()) {
            return;
        }
        if (false == this.getIsChoice()) {
            this.actPlayChoice();
        } else {
            this.actRemoveChoice();
        }
    },
    doChoiceState(_isChoice) {
        if (this.isLock()) {
            return;
        }
        if (true == _isChoice) {
            this.actPlayChoice();
        } else {
            this.actRemoveChoice();
        }
    },
    resetPosition() {
        this.setPosition(this.getPositionActual())
    },
    doLockChoice() {
        this.lock = true;
    },
    doUnLockChoice() {
        this.lock = false;
    },
    isLock() {
        return this.lock;
    },
    /***检查是否碰撞到*/
    isCheckCollision(_posMouse) {
        // cc.error(_posMouse);
        var isX = this.isCollisionX(_posMouse.x);
        var isY = this.isCollisionY(_posMouse.y);
        var b = !!isX && !!isY;
        return b;
    },
    /***设置碰撞尺寸 */
    setCollisionSize(_size) {
        this.collisionSize = _size;
    },
    /***是否进入X轴碰撞 */
    isCollisionX(_x) {
        var selfSize = this.getContentSize();
        var selfX = this.getWorldPos().x;
        var leftX = selfX - selfSize.width * 0.5;
        var rightX = selfX - selfSize.width * 0.5 + this.collisionSize.width;
        var b = leftX < _x && rightX > _x;
        return b;
    },
    /***是否进入Y轴碰撞 */
    isCollisionY(_y) {
        var selfSize = this.getContentSize();
        var selfY = this.getWorldPos().y;
        var bottomY = selfY - selfSize.height * 0.5;
        var topY = selfY + selfSize.height * 0.5;
        var b = bottomY < _y && topY > _y;
        return b;
    }
});